package org.jwt.oauth.user;

import java.util.Map;

import org.jwt.oauth.tools.JsonTool;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

public class FusionUserData {
	public static final String ANONY_MOUS_USER = "anonymousUser";

	public static FusionUserDetails getUserDetails() {
		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
		if (authentication == null) {
			return null;
		}
		Object principal = authentication.getPrincipal();
		if (principal == null || ANONY_MOUS_USER.equalsIgnoreCase(principal.toString())) {
			return null;
		}
		FusionUserDetails userData = (FusionUserDetails) principal;
		return userData;
	}

	public static Map<String, Object> getLoginInfo() {
		FusionUserDetails userDetails = getUserDetails();
		return userDetails.getDataMap();
	}

	public static <T> T getLoginInfo(Class<T> clazz) {
		FusionUserDetails userDetails = getUserDetails();
		Map<String, Object> dataMap = userDetails.getDataMap();
		if (dataMap != null && dataMap.size() > 0) {
			String objectToJson = JsonTool.ObjectToJson(dataMap);
			return JsonTool.jsonToClass(objectToJson, clazz);
		}
		return null;
	}

}
